package com.gem.growingtree.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.gem.growingtree.activity.vo.User;
import com.gem.growingtree.utils.HttpUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class RegisterActivity extends Activity {

	@ViewInject(R.id.register_name)
	private EditText register_name;
	@ViewInject(R.id.register_password)
	private EditText register_password;
	@ViewInject(R.id.register_password1)
	private EditText register_password1;
	@ViewInject(R.id.register_phone)
	private EditText register_phone;
	@ViewInject(R.id.register_email)
	private EditText register_email;

	@ViewInject(R.id.register_grant)
	private RadioGroup register_grant;
	@ViewInject(R.id.register_address)
	private EditText register_address;

	// 验证码
	ImageView vc_image;
	Button vc_shuaixi;
	String getCode = null;
	EditText vc_code;
	// @ViewInject((R.id.register_grant.getCheckedRadioButtonId()))
	// private RadioButton grantRadioButton;

	Map<String, Object> map = new HashMap<String, Object>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_register);
		ViewUtils.inject(this);

		// 验证#############
		// register_address.setError("请输入地址");
		vc_image = (ImageView) findViewById(R.id.vc_image);
		vc_image.setImageBitmap(com.gem.growingtree.code.Code.getInstance()
				.getBitmap());
		vc_code = (EditText) findViewById(R.id.vc_code);

		getCode = com.gem.growingtree.code.Code.getInstance().getCode(); // 获取显示的验证码
		Log.e("info", getCode + "----");
		vc_shuaixi = (Button) findViewById(R.id.vc_shuaixi);
		vc_shuaixi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				vc_image.setImageBitmap(com.gem.growingtree.code.Code
						.getInstance().getBitmap());
				getCode = com.gem.growingtree.code.Code.getInstance().getCode();
			}
		});

		// register();
	}

	// 注册事件
	@OnClick(R.id.registerButton)
	public void register(View v) {
		// 获取验证码
		String v_code = vc_code.getText().toString().trim();
		if (v_code == null || v_code.equals("")) {
			Toast.makeText(RegisterActivity.this, "没有填写验证码", 2).show();
		} else if (!v_code.equals(getCode)) {
			Toast.makeText(RegisterActivity.this, "验证码填写不正确，请重新填写", 2).show();
		} else {
			// 验证码正确后
			String password = register_password.getText().toString();
			String password1 = register_password1.getText().toString();
			String name = register_name.getText().toString();
			String phone = register_phone.getText().toString();
			String email = register_email.getText().toString();
			// public String grant =
			// ((RadioButton)(register_grant.getCheckedRadioButtonId())).getText().toString();

			// public String grant = register_grant.getText().toString();

			// 获取权限的
			RadioButton radioButton = (RadioButton) findViewById(register_grant
					.getCheckedRadioButtonId());
			String grant1 = radioButton.getText().toString();
			String grant = null;
			if (grant1.equals("家长")) {
				grant = "P";
			} else {
				grant = "T";
			}

			String address = register_address.getText().toString();

			if (password != null && password1 != null
					&& password.equals(password1)) {
				// User user = new User(name, phone, email, password, address,
				// grant);
				map.put("nickName", name);
				map.put("cellPhone", phone);
				map.put("password", password);
				map.put("role", grant);
				map.put("address", address);
				map.put("email", email);
				// map.put("user", user);
			} else {
				Toast.makeText(RegisterActivity.this, "密码不一致，请重新输入",
						Toast.LENGTH_LONG).show();
			}
			String url = HttpUtil.urlPath + "ws/register/main";
			try {
				String result = HttpUtil.postRequest(url, map);
				JSONObject jsonObject = new JSONObject(result);

				String a = (String) jsonObject.get("success");

				if (a.equals("1")) {
					// Bundle bundle=new Bundle();
					// bundle.putString("phone", phone);
					// bundle.putString("password", password);

					Log.i("AA", "成功过！！！！！！！！！！！！！！！！！");
					Intent intent = new Intent(RegisterActivity.this,
							LoginActivity.class);
					intent.putExtra("phone", phone);
					intent.putExtra("password", password);
					startActivity(intent);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}

}
